package com.fs.ibox.web;

import com.fs.ibox.pojo.Collection;
import com.fs.ibox.service.CollectionService;
import com.fs.ibox.util.BeanFactory;
import com.fs.ibox.util.DateUtil;
import com.fs.ibox.util.ResponseDataUtil;
import com.fs.ibox.util.ResponseJSONUtil;
import com.fs.ibox.vo.ResponseData;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet(name = "VpiServlet", value = "/VpiServlet/*")
public class VpiServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        String path = request.getPathInfo();
        if("/queryAll".equals(path)){
            queryAll(request,response);
        }
        else if("/add".equals(path)){
            try {
                add(request,response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else if("/queryById".equals(path)){
            queryById(request,response);
        }


    }

    private void queryById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");

        int intId=Integer.parseInt(id);
//        Integer intId=Integer.getInteger(id);
        CollectionService bean = BeanFactory.createBean(CollectionService.class);
        List<Collection> collections = bean.selectById(intId);
        ResponseData responseData = ResponseDataUtil.buildOk(collections);
        ResponseJSONUtil.responseJSON(responseData,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     doGet(request,response);
    }
    private void queryAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CollectionService bean = BeanFactory.createBean(CollectionService.class);
        List<Collection> collections = bean.selectAll();
        ResponseData responseData = ResponseDataUtil.buildOk(collections);
        ResponseJSONUtil.responseJSON(responseData,response);
    }
    private void add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Collection collection = new Collection();
        String cname = request.getParameter("cname");
        String type = request.getParameter("type");
        int inttype=Integer.parseInt(type);
        String releasenum = request.getParameter("releasenum");
        int intnum=Integer.parseInt(releasenum);
        String price = request.getParameter("price");
        double intprice=Double.parseDouble(price);
        String content = request.getParameter("content");
        String startdate = request.getParameter("startdate");
        Date date = DateUtil.toDate(startdate);
        String enddate = request.getParameter("enddate");
        Date date1 = DateUtil.toDate(enddate);
        String cimg = request.getParameter("cimg");
        String residual = request.getParameter("residual");
        int intresidual=Integer.parseInt(residual);
        collection.setCname(cname);
        collection.setType(inttype);
        collection.setReleasenum(intnum);
        collection.setPrice(intprice);
        collection.setContent(content);
        collection.setStartdate(date);
        collection.setEnddate(date1);
        collection.setCimg(cimg);
        collection.setResidual(intresidual);
        CollectionService bean = BeanFactory.createBean(CollectionService.class);
        bean.addcollection(collection);


    }

}
